@@ -54,7 +54,7 @@ module Agents |
||
54 | 54 |
def receive(incoming_events) |
55 | 55 |
incoming_events.each do |event| |
56 | 56 |
payload_interpolated = interpolated(event) |
57 |
- user_credentails = payload_interpolated['user_credentials'] || credential('boxcar_api_key') |
|
57 |
+ user_credentials = payload_interpolated['user_credentials'] || credential('boxcar_api_key') |
|
58 | 58 |
post_params = { |
59 | 59 |
'user_credentials' => user_credentials, |
60 | 60 |
'notification' => { |
@@ -0,0 +1,61 @@ |
||
1 |
+require 'rails_helper' |
|
2 |
+ |
|
3 |
+describe Agents::BoxcarAgent do |
|
4 |
+ before(:each) do |
|
5 |
+ @valid_params = { |
|
6 |
+ 'user_credentials' => 'access_token', |
|
7 |
+ 'title' => 'Sample Title', |
|
8 |
+ 'body' => 'Sample Body' |
|
9 |
+ } |
|
10 |
+ @checker = Agents::BoxcarAgent.new(:name => "boxcartest", :options => @valid_params) |
|
11 |
+ @checker.user = users(:bob) |
|
12 |
+ @checker.save! |
|
13 |
+ |
|
14 |
+ @event = Event.new |
|
15 |
+ @event.agent = agents(:bob_weather_agent) |
|
16 |
+ @event.payload = { :body => 'Sample message' } |
|
17 |
+ @event.save! |
|
18 |
+ end |
|
19 |
+ |
|
20 |
+ describe 'validating' do |
|
21 |
+ before do |
|
22 |
+ expect(@checker).to be_valid |
|
23 |
+ end |
|
24 |
+ |
|
25 |
+ it "should require access token" do |
|
26 |
+ @checker.options['user_credentials'] = nil |
|
27 |
+ expect(@checker).not_to be_valid |
|
28 |
+ end |
|
29 |
+ end |
|
30 |
+ |
|
31 |
+ describe '#working?' do |
|
32 |
+ it "should not be working until the first event was received" do |
|
33 |
+ expect(@checker).not_to be_working |
|
34 |
+ @checker.last_receive_at = Time.now |
|
35 |
+ expect(@checker).to be_working |
|
36 |
+ end |
|
37 |
+ end |
|
38 |
+ |
|
39 |
+ describe "#receive" do |
|
40 |
+ |
|
41 |
+ it "sends a message" do |
|
42 |
+ stub(HTTParty).post '{ |
|
43 |
+ "id":843878764, |
|
44 |
+ "message":"blah", |
|
45 |
+ "title":"blah", |
|
46 |
+ "source_name":"Huginn" |
|
47 |
+ }' |
|
48 |
+ @checker.receive([@event]) |
|
49 |
+ end |
|
50 |
+ |
|
51 |
+ it "should raise error when response says unauthorized" do |
|
52 |
+ stub(HTTParty).post '{"Response":"Not authorized"}' |
|
53 |
+ expect{@checker.send_notification}.to raise_error |
|
54 |
+ end |
|
55 |
+ |
|
56 |
+ it "should raise error when response has an error" do |
|
57 |
+ stub(HTTParty).post '{"error": {"message": "Sample error"}}' |
|
58 |
+ expect{@checker.send_notification}.to raise_error |
|
59 |
+ end |
|
60 |
+ end |
|
61 |
+end |